package server;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

/*
 * Author: David Swords
 * Email: davidswordster@gmail.com
*/

@SuppressWarnings("serial")
public class RMIServerImpl extends UnicastRemoteObject implements RMIServer
{
	RMIServerImpl() throws RemoteException{
 		super();
  	}

	public Vector<String[]> listOfSongs() throws RemoteException
	{
		Vector<String[]> songList = new Vector<String[]>();
		String[] tags = {"<artist>","</artist>","<album>","</album>","<title>","</title>","<location>","</location>","<duration>","</duration>"};
		String[] container = new String[5];
		songList.removeAllElements();
		
		try
		{
			File file = new File("./src/ClientTests/tester.xml");
			FileInputStream fis = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(fis);
			DataInputStream dis = new DataInputStream(bis);
			String line;
			int start, finish;
			while (dis.available() != 0)
			{
				line = dis.readLine();
				for(int i=0,j=0;i<tags.length;i+=2,j++){
					start = line.indexOf((String)tags[i])+tags[i].length();
					finish = line.indexOf(tags[i+1]);
					container[j]=line.substring(start, finish);
					//System.out.println(container[j]);
				}
				songList.addElement(container);
			}	
			fis.close();
			bis.close();
			dis.close();
		}
		catch (RemoteException exc){
		  	System.out.println("Error in lookup: " + exc.toString());
		}
		catch (java.net.MalformedURLException exc){
		  	System.out.println("Malformed URL: " + exc.toString());
		}
		catch (FileNotFoundException e) {
			System.out.println("File not found: "+e.toString());
		}
		catch (IOException e) {
		    System.out.println("IOException: "+e.toString());
		}
		return songList;
	}

	/* (non-Javadoc)
	 * @see ClientTests.RMIServer#getPlaylist()
	 */
	public Vector<String[]> getPlaylist() throws RemoteException
	{
		Vector<String[]> playList = new Vector<String[]>();
		String[] tags = {"<artist>","</artist>","<album>","</album>","<title>","</title>","<location>","</location>","<duration>","</duration>"};
		String[] container = new String[5];
		playList.removeAllElements();
		
		try
		{
			File file = new File("./src/ClientTests/playlist.xml");
			FileInputStream fis = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(fis);
			DataInputStream dis = new DataInputStream(bis);
			String line;
			int start, finish;
			while (dis.available() != 0)
			{
				line = dis.readLine();
				for(int i=0,j=0;i<tags.length;i+=2,j++){
					start = line.indexOf((String)tags[i])+tags[i].length();
					finish = line.indexOf(tags[i+1]);
					container[j]=line.substring(start, finish);
					//System.out.println(container[j]);
				}
				playList.addElement(container);
			}	
			fis.close();
			bis.close();
			dis.close();
		}
		catch (RemoteException exc){
		  	System.out.println("Error in lookup: " + exc.toString());
		}
		catch (java.net.MalformedURLException exc){
		  	System.out.println("Malformed URL: " + exc.toString());
		}
		catch (FileNotFoundException e) {
			System.out.println("File not found: "+e.toString());
		}
		catch (IOException e) {
		    System.out.println("IOException: "+e.toString());
		}
		return playList;
	}
	
	public boolean addSongToPlaylist(final String[] s) throws RemoteException
	{	
		System.out.println("Congrats, song added to playlist.");
		return true;
	}

	/* (non-Javadoc)
	 * @see client.RMIServer#removeSongToPlaylist(java.lang.String[])
	 */
	public boolean removeSongToPlaylist(String[] s) throws RemoteException
	{
		System.out.println("Congrats, song removed from playlist.");
		return true;
	}
}